{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!
}}{{#let function_comma = (if function:params.fields? ", " null)}}
#if FOLLY_HAS_COROUTINES
{{#if function:sink?}}
folly::coro::Task<{{> types/return_type_client}}> {{> service_common/client_class_name}}::co_{{function:cpp_name}}({{> service_common/function_param_list_client}}) {
  ::apache::thrift::RpcOptions rpcOptions;
  co_return co_await co_{{function:cpp_name}}(rpcOptions{{function_comma}}{{> service_common/param_list}});
}
folly::coro::Task<{{> types/return_type_client}}> {{> service_common/client_class_name}}::co_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function_comma}}{{> service_common/function_param_list_client}}) {
  const folly::CancellationToken& cancelToken =
        co_await folly::coro::co_current_cancellation_token;
  const bool cancellable = cancelToken.canBeCancelled();
  apache::thrift::ClientReceiveState returnState;
  apache::thrift::ClientCoroCallback<false> callback(&returnState, co_await folly::coro::co_current_executor);
  auto protocolId = apache::thrift::GeneratedAsyncClient::getChannel()->getProtocolId();
  auto [ctx, header] = {{function:cpp_name}}Ctx(&rpcOptions);
  using CancellableCallback = apache::thrift::CancellableRequestClientCallback<false>;
  auto cancellableCallback = cancellable ? CancellableCallback::create(&callback, channel_) : nullptr;
  auto wrappedCallback = {{#if function:stream?}}apache::thrift::createBiDiClientCallback{{#else}}apache::thrift::createSinkClientCallback{{/if}}(
    apache::thrift::RequestClientCallback::Ptr(apache::thrift::RequestClientCallback::Ptr(cancellableCallback ? (apache::thrift::RequestClientCallback*)cancellableCallback.get() : &callback)));
{{#function:creates_interaction?}}
  {{function:created_interaction.cpp_name}} interactionHandle(channel_, "{{function:created_interaction.cpp_name}}", interceptors_);
{{/function:creates_interaction?}}

  if (ctx != nullptr) {
    auto argsAsRefs = std::tie({{> service_common/param_list}});
    ctx->processClientInterceptorsOnRequest(apache::thrift::ClientInterceptorOnRequestArguments(argsAsRefs), header.get(), rpcOptions).throwUnlessValue();
  }

  fbthrift_serialize_and_send_{{function:cpp_name}}(rpcOptions, std::move(header), ctx.get(), wrappedCallback{{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}}{{function_comma}}{{> service_common/param_list}});

  if (cancellable) {
    folly::CancellationCallback cb(cancelToken, [&] { CancellableCallback::cancel(std::move(cancellableCallback)); });
    co_await callback.co_waitUntilDone();
  } else {
    co_await callback.co_waitUntilDone();
  }

  if (ctx != nullptr) {
    ctx->processClientInterceptorsOnResponse(returnState.header(), returnState.exception()).throwUnlessValue();
  }
  if (returnState.isException()) {
    co_yield folly::coro::co_error(std::move(returnState.exception()));
  }
  returnState.resetProtocolId(protocolId);
  returnState.resetCtx(std::move(ctx));
{{^function:creates_interaction?}}
  co_return recv_{{function:cpp_name}}(returnState);
{{/function:creates_interaction?}}
{{#function:creates_interaction?}}
  co_return std::make_pair(
    std::move(interactionHandle),
    recv_{{function:cpp_name}}(returnState)
  );
{{/function:creates_interaction?}}
}
{{/if function:sink?}}{{!
Other coroutine functions are inline templates to prevent binary size regression
}}
#endif // FOLLY_HAS_COROUTINES
